package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.PapersAndBooksMapper;
import com.ruoyi.business.domain.PapersAndBooks;
import com.ruoyi.business.service.IPapersAndBooksService;

import javax.validation.Validator;

/**
 * 论文著作Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-18
 */
@Service
public class PapersAndBooksServiceImpl  extends BaseServiceImpl implements IPapersAndBooksService
{
    @Autowired
    private PapersAndBooksMapper papersAndBooksMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询论文著作
     * 
     * @param id 论文著作主键
     * @return 论文著作
     */
    @Override
    public PapersAndBooks selectPapersAndBooksById(Long id)
    {
        return papersAndBooksMapper.selectPapersAndBooksById(id);
    }

    /**
     * 查询论文著作列表
     * 
     * @param papersAndBooks 论文著作
     * @return 论文著作
     */
    @Override
    public List<PapersAndBooks> selectPapersAndBooksList(PapersAndBooks papersAndBooks)
    {
        return papersAndBooksMapper.selectPapersAndBooksList(papersAndBooks);
    }

    /**
     * 新增论文著作
     * 
     * @param papersAndBooks 论文著作
     * @return 结果
     */
    @Override
    public int insertPapersAndBooks(PapersAndBooks papersAndBooks)
    {
        papersAndBooks.setCreateTime(DateUtils.getNowDate());
        return papersAndBooksMapper.insertPapersAndBooks(papersAndBooks);
    }

    /**
     * 修改论文著作
     * 
     * @param papersAndBooks 论文著作
     * @return 结果
     */
    @Override
    public int updatePapersAndBooks(PapersAndBooks papersAndBooks)
    {
        papersAndBooks.setUpdateTime(DateUtils.getNowDate());
        return papersAndBooksMapper.updatePapersAndBooks(papersAndBooks);
    }

    /**
     * 批量删除论文著作
     * 
     * @param ids 需要删除的论文著作主键
     * @return 结果
     */
    @Override
    public int deletePapersAndBooksByIds(Long[] ids)
    {
        return papersAndBooksMapper.deletePapersAndBooksByIds(ids);
    }

    /**
     * 删除论文著作信息
     * 
     * @param id 论文著作主键
     * @return 结果
     */
    @Override
    public int deletePapersAndBooksById(Long id)
    {
        return papersAndBooksMapper.deletePapersAndBooksById(id);
    }

    @Override
    public String importData(List<PapersAndBooks> papersList, boolean updateSupport, String operName) {
    if (papersList == null || papersList.isEmpty()) {
        throw new ServiceException("导入论文和书籍数据不能为空！");
    }

    int successNum = 0;
    int failureNum = 0;
    StringBuilder successMsg = new StringBuilder();
    StringBuilder failureMsg = new StringBuilder();
        List<String>failureNums = new ArrayList<>();

        int i = 1;
    for (PapersAndBooks paper : papersList) {
        try {
            i++;
            if (paper.getAwardYear() == null || paper.getAwardYear().equals("") || paper.getEmployeeId() ==null || paper.getEmployeeId().equals("") || !isAwardYearValid(paper.getAwardYear())) {
                failureNums.add(String.valueOf(i));
                failureNum++;
                continue ;
            }
            // 验证是否存在这篇论文或书籍
            PapersAndBooks existingPaper = papersAndBooksMapper.selectPapersAndBooksById(paper.getId());
            if (existingPaper == null) {
                // 验证数据
                BeanValidators.validateWithException(validator, paper);
                // 设置初始值
                paper.setCreateTime(DateUtils.getNowDate());
                paper.setCreateBy(operName);
                papersAndBooksMapper.insertPapersAndBooks(paper);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、论文或书籍 " + paper.getTitle() + " 导入成功");
            } else if (updateSupport) {
                // 验证数据
                BeanValidators.validateWithException(validator, paper);
                paper.setId(existingPaper.getId());
                paper.setUpdateTime(DateUtils.getNowDate());
                paper.setUpdateBy(operName);
                papersAndBooksMapper.updatePapersAndBooks(paper);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、论文或书籍 " + paper.getTitle() + " 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、论文或书籍 " + paper.getTitle() + " 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、论文或书籍 " + paper.getTitle() + " 导入失败：";
            failureMsg.append(msg + e.getMessage());
        }
    }

    if (failureNum > 0) {
        failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
        //throw new ServiceException(failureMsg.toString());
    } else {
        successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
    }
        setCacheInfo("论文或书籍",String.valueOf(successNum),String.valueOf(failureNum),failureNums.stream().collect(Collectors.joining(",")));

    return successMsg.toString() + failureMsg.toString();
}

}
